const ll inf = 1e18;
struct SegNode {
	ll sum, lmx, rmx, tmx, mx;
  SegNode() : sum(-inf), lmx(0), rmx(0), tmx(0), mx(-inf) {}
  SegNode(ll v): sum(0), lmx(0), rmx(0), tmx(0), mx(0) {}
};

SegNode operator+(SegNode lhs, SegNode rhs) {
	SegNode t;
	t.sum = lhs.sum + rhs.sum;
  t.tmx = max(lhs.tmx, rhs.tmx);
  t.lmx = max(lhs.lmx, lhs.sum + rhs.lmx);
  t.lmx = max(t.lmx, lhs.sum + rhs.sum);
  t.rmx = max(rhs.rmx, rhs.sum + lhs.rmx);
  t.rmx = max(t.rmx, lhs.sum + rhs.sum);
  t.tmx = max(t.tmx, lhs.rmx + rhs.lmx);
  t.tmx = max(t.tmx, lhs.rmx + rhs.sum);
  t.tmx = max(t.tmx, lhs.sum + rhs.lmx);
  t.mx = max(lhs.mx, rhs.mx);
	return t;
};

void solve()
{
  int n, m;
  cin >> n >> m;

  vector<vector<pii>> opers(n + 2);
  vector<SegNode> V(m + 1);
  rep(i, 1, m) {
    int l, r, k;
    cin >> l >> r >> k;
    opers[l].pb({i, k});
    opers[r + 1].pb({i, 0});
    V[i] = SegNode(0ll);
  }

  SegTree<SegNode> tr(V);
  int q;
  cin >> q;
  vector<vector<tuple<int, int, int>>> querys(n + 2);
  rep(i, 1, q) {
    int l, r, k;
    cin >> k >> l >> r;
    querys[k].pb({l, r, i});
  }

  vl ans(q + 1), val(m + 1);
  rep(i, 1, n) {
    for (auto &[l, v]: opers[i]) {
      SegNode t;
      t.lmx = t.rmx = t.sum = t.tmx = t.mx = v;
      t.tmx = t.lmx = t.rmx = max(0ll, t.tmx);
      tr.modify(l, t);
    }
    for (auto &[l, r, id]: querys[i]) {
      auto t = tr.query(l, r);
      ans[id] = t.mx < 0 ? t.mx : t.tmx;
    }
  }

  rep(i, 1, q) {
    cout << ans[i] << '\n';
  }
}